#pragma once
#include <pthread.h>
#include "Dispatcher.h"
#include "ChannelMap.h"

extern struct Dispatcher EpollDispatcher;
extern struct Dispatcher PollDispatcher;
extern struct Dispatcher SelectDispatcher;

enum ElemType{ADD, DELECT, MODIFY};

enum EventLoopModelType{EPOLL, POLL, SELECT};

struct ChannelElement
{   
    enum ElemType type;
    struct Channel *channel;
    struct ChannelElement *next;
};


struct EventLoop
{
    bool isRun;
    struct Dispatcher *dispatcher;
    void *dispatcherData;
    //任务队列
    struct ChannelElement *head;
    struct ChannelElement *tail;
    //map
    ChannelMap *channelMap;
    //线程id， name
    pthread_t threadID;
    const char *threadName;
    pthread_mutex_t mutex;
    int32_t socketPair[2];  //存储本地通信的fd 通过socketpair()初始化  socketPair[0]发送数据    socketPair[1]接收数据
};

//初始化
struct EventLoop *eventLoopInit(enum EventLoopModelType type);//主线程
struct EventLoop *eventLoopInitEx(char *threadName, enum EventLoopModelType type);//子线程
int8_t eventLoopRun(struct EventLoop *evLoop);
//处理被激活的文件fd
int8_t eventActivate(struct EventLoop* evLoop, int32_t fd, int32_t event);
//任务队列添加任务
int8_t eventLoopAddTask(struct EventLoop *evLoop, struct Channel *channel, enum ElemType type);
//处理任务队列中的任务
int8_t evebtLoopProcessTask(struct EventLoop *evLoop);
//处理dispatcher中的节点
int8_t eventLoopAdd(struct EventLoop *evLoop, struct Channel *channel);
int8_t eventLoopRemove(struct EventLoop *evLoop, struct Channel *channel);
int8_t eventLoopModify(struct EventLoop *evLoop, struct Channel *channel);
//释放channel
int8_t destroyChannel(struct EventLoop *evLoop, struct Channel *channel);